/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.cspimp.psp.service.impl;

import me.zhengjie.modules.cspimp.psp.domain.OmCompanyCredential;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.cspimp.psp.repository.OmCompanyCredentialRepository;
import me.zhengjie.modules.cspimp.psp.service.OmCompanyCredentialService;
import me.zhengjie.modules.cspimp.psp.service.dto.OmCompanyCredentialDto;
import me.zhengjie.modules.cspimp.psp.service.dto.OmCompanyCredentialQueryCriteria;
import me.zhengjie.modules.cspimp.psp.service.mapstruct.OmCompanyCredentialMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://docs.auauz.net
* @description 服务实现
* @author zmm
* @date 2020-05-30
**/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class OmCompanyCredentialServiceImpl implements OmCompanyCredentialService {

    private final OmCompanyCredentialRepository omCompanyCredentialRepository;
    private final OmCompanyCredentialMapper omCompanyCredentialMapper;

    @Override
    public Map<String,Object> queryAll(OmCompanyCredentialQueryCriteria criteria, Pageable pageable){
        Page<OmCompanyCredential> page = omCompanyCredentialRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(omCompanyCredentialMapper::toDto));
    }

    @Override
    public List<OmCompanyCredentialDto> queryAll(OmCompanyCredentialQueryCriteria criteria){
        return omCompanyCredentialMapper.toDto(omCompanyCredentialRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public OmCompanyCredentialDto findById(Long id) {
        OmCompanyCredential omCompanyCredential = omCompanyCredentialRepository.findById(id).orElseGet(OmCompanyCredential::new);
        ValidationUtil.isNull(omCompanyCredential.getId(),"OmCompanyCredential","id",id);
        return omCompanyCredentialMapper.toDto(omCompanyCredential);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmCompanyCredentialDto create(OmCompanyCredential resources) {
        resources.setCid(0l);
        return omCompanyCredentialMapper.toDto(omCompanyCredentialRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(OmCompanyCredential resources) {
        OmCompanyCredential omCompanyCredential = omCompanyCredentialRepository.findById(resources.getId()).orElseGet(OmCompanyCredential::new);
        ValidationUtil.isNull( omCompanyCredential.getId(),"OmCompanyCredential","id",resources.getId());
        omCompanyCredential.copy(resources);
        omCompanyCredentialRepository.save(omCompanyCredential);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            omCompanyCredentialRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<OmCompanyCredentialDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (OmCompanyCredentialDto omCompanyCredential : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("证书名称", omCompanyCredential.getName());
            map.put("文件地址", omCompanyCredential.getFilePath());
            map.put("创建者", omCompanyCredential.getCreateBy());
            map.put("更新者", omCompanyCredential.getUpdateBy());
            map.put("创建日期", omCompanyCredential.getCreateTime());
            map.put("更新时间", omCompanyCredential.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}